package com.cucn.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cucn.eduservice.entity.EduSubject;
import com.cucn.eduservice.entity.excel.SubjectData;
import com.cucn.eduservice.entity.subject.OneSubject;
import com.cucn.eduservice.entity.subject.TwoSubject;
import com.cucn.eduservice.listener.SubjectExcelListener;
import com.cucn.eduservice.mapper.EduSubjectMapper;
import com.cucn.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author KouChaoJie
 * @since 2021-09-28
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    /**
     * @param file           上传来的excel文件
     * @param subjectService SubjectService类,方便Excel监听器进行数据库操作
     */
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            //得到文件输入流
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询所有一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //查询所有二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建最终存储封装数据的list
        List<OneSubject> finalSubject = new ArrayList<>();

        //封装一级分类
        for (EduSubject eduSubject : oneSubjectList) {
            //将eduSubject值取出,放入OneSubject对象里
            OneSubject oneSubject = new OneSubject();
            //相当于将一个对象中的值get出,然后再set进另一个对象
            BeanUtils.copyProperties(eduSubject, oneSubject);
            //放到最终封装list里
            finalSubject.add(oneSubject);

            //创建list集合封装每个一级分类的二级分类
            List<TwoSubject> twoFinalSubject = new ArrayList<>();
            //在一级分类循环里遍历查询所有的二级分类
            for (EduSubject eduTwoSubject : twoSubjectList) {
                //判断二级分类属于哪个一级分类
                if (eduTwoSubject.getParentId().equals(oneSubject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduTwoSubject, twoSubject);
                    twoFinalSubject.add(twoSubject);
                }
            }

            //把一级下面所有的二级分类放入对应的一级分类下
            oneSubject.setChildren(twoFinalSubject);
        }

        return finalSubject;
    }
}
